<template>
  <a-modal
    v-model="visibleSet"
    title="赏金规则设置"
    @ok="handleOk"
    :confirm-loading="confirmLoading"
    @cancel="$emit('update:visibleSet', false)"
  >
    <a-form :form="form">
      <a-form-item>
        <a-textarea
          placeholder="请输入赏金规则"
          :rows="4"
          v-decorator="['brokerRule', { rules: [{ required: true, message: '赏金规则不能为空' }] }]"
        />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { postSaveBrokerOrEmpPost, putRecruitInfos } from "../api";
export default {
  props: {
    visibleSet: {
      type: Boolean,
      default: false
    },
    selectedListTempString: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: ""
    },
    val: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      form: this.$form.createForm(this, {}),
      confirmLoading: false
    };
  },
  watch: {
    visibleSet(v) {
      if (v) {
        setTimeout(() => {
          this.form.setFieldsValue({
            brokerRule: this.val
          });
        });
      } else {
        this.form.resetFields();
      }
    }
  },

  methods: {
    async onPostSaveBrokerOrEmpPost(v) {
      try {
        await postSaveBrokerOrEmpPost({
          brokerRule: v.brokerRule,
          recruitInfoIds: this.selectedListTempString,
          copyType: 1
        });
        this.$notification.success({ message: "添加成功" });
        this.$emit("upDate");
        this.$emit("update:visibleSet", false);
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    async onPutRecruitInfos(v) {
      try {
        await putRecruitInfos({
          brokerRule: v.brokerRule,
          id: this.id
        });
        this.$notification.success({ message: "修改成功" });
        this.$emit("upDate");
        this.$emit("update:visibleSet", false);
      } catch (err) {
        console.log("error log --> ", err);
      }
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          if (this.id) {
            this.onPutRecruitInfos(values);
          } else {
            this.onPostSaveBrokerOrEmpPost(values);
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped></style>
